<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>2. 物理世界 | 被删的前端游乐场</title>
    <meta name="description" content="Just playing around">
    
    
    <link rel="preload" href="/front-end-playground/assets/css/0.styles.a683ed26.css" as="style"><link rel="preload" href="/front-end-playground/assets/js/app.e5cf4092.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/3.c853767c.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/2.eed49fdd.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/63.d0b42c5b.js" as="script">
    <link rel="stylesheet" href="/front-end-playground/assets/css/0.styles.a683ed26.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/front-end-playground/" class="home-link router-link-active"><!----> <span class="site-name">被删的前端游乐场</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端基础</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端基础</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>玩转 Angular</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Cycle.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>D3.js 和 Tree</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Three.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:10px;"><div class="kitty-main" data-v-0cd75e06><span class="stand" data-v-0cd75e06></span> <div class="cat" data-v-0cd75e06><div class="body" data-v-0cd75e06></div> <div class="head" data-v-0cd75e06><div class="ear" data-v-0cd75e06></div> <div class="ear" data-v-0cd75e06></div></div> <div class="face" data-v-0cd75e06><div class="nose" data-v-0cd75e06></div> <div class="whisker-container" data-v-0cd75e06><div class="whisker" data-v-0cd75e06></div> <div class="whisker" data-v-0cd75e06></div></div> <div class="whisker-container" data-v-0cd75e06><div class="whisker" data-v-0cd75e06></div> <div class="whisker" data-v-0cd75e06></div></div></div> <div class="tail-container" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06></div></div></div></div></div></div></div></div></div></div> <p class="sidebar-heading open"><span>BOX2D.js 文档与教程</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/front-end-playground/front-end-others/box2d-tutorial/1-0-basic-catalog.html" class="sidebar-link">1. 基本概念</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/2-0-world.html" class="active sidebar-link">2. 物理世界</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/2-0-world.html#物理世界" class="sidebar-link">物理世界</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/box2d-tutorial/2-0-world.html#b2world" class="sidebar-link">b2World</a></li></ul></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/3-0-shape-and-body.html" class="sidebar-link">3. 形状 shape - 刚体 body</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/4-0-joint.html" class="sidebar-link">4. 关节 joint</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/5-0-operation.html" class="sidebar-link">5. 操作 operation</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-1-create-and-init-world.html" class="sidebar-link">6-1. 创建世界并初始化</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-2-add-body.html" class="sidebar-link">6-2. 添加刚体</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-3-add-bound.html" class="sidebar-link">6-3. 添加边界</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-4-mouse-operate-body.html" class="sidebar-link">6-4. 鼠标操作刚体</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/6-5-handle-contact.html" class="sidebar-link">6-5. 处理碰撞刚体</a></li><li><a href="/front-end-playground/front-end-others/box2d-tutorial/7-0-api.html" class="sidebar-link">7. api</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><p>第二章介绍物理世界，包括世界初始化类，以及创建一个世界。</p> <h2 id="物理世界"><a href="#物理世界" aria-hidden="true" class="header-anchor">#</a> 物理世界</h2> <h3 id="世界初始化"><a href="#世界初始化" aria-hidden="true" class="header-anchor">#</a> 世界初始化</h3> <p>每个 Box2D 程序都是从一个世界对象(world object)的创建开始的，它是一个管理内存，对象和模拟的中心，在这个世界中，刚体将遵循物理规律运动。</p> <p>所以，我们首先需要创建一个世界，世界初始化类即用于创建一个世界对象，并设定有关世界的初始参数。</p> <ul><li>创建物理世界</li></ul> <p>利用 Box2D 开发程序时，首先要创建一个世界对象。它负责管理内部一切对象的内存和模拟过程。</p> <ul><li>添加物理边界</li></ul> <p>要创建一个世界中的对象，首先要为世界定义边界区域，Box2D 针对区域内的所有对象进行模拟碰撞，区域的大小并不重要，但更适合的区域将提高程序性能，一般来讲这个区域设置的要比演示区域更大一些，因为一旦对象在运动时到达了边界，它就会被“冻结”并停止一切模拟活动。</p> <ul><li>添加基本力--重力</li></ul> <p>然后要为这个世界设置重力，用向量<code>b2Vec2(x,y)</code>来表示的，x 代表水平运动，正数向右，负数向左，y 代表垂直运动，正数向下，负数向上；</p> <p>同时需要定义一个布尔型参数用来表示是否允许睡眠，在这个世界中生成的一切对象的模拟效果都是实时计算出来的，当 doSleep=false 的时候，即使物体停止了运动，计算机还是在不停的进行着运算，其实这是完全不必要的，所以一般都设为 true，这样当物体停止之后就不会进行无谓的 cpu 消耗了。</p> <ul><li>添加物理对象</li></ul> <p>参数都准备好之后，传入 b2World 对象中并将其实例化，这样一个物理引擎的模拟区域就做好了，可以开始加入模拟的对象了。
基本步骤为：</p> <ol><li>创建并定义刚体位置；</li> <li>给刚体定义皮肤；</li> <li>用世界对象添加刚体实例;</li> <li>根据皮肤形状创建模拟图形类：摩擦力、密度、弹力等等；</li> <li>在刚体上添加模拟图形实例；</li> <li>根据刚体的密度和面积计算出质量，密度*面积=质量。</li></ol> <ul><li>更新状态</li></ul> <p>然后，就可以通过监听帧频率而不断刷新实现让所有对象模拟运动，把上面那两个参数传入世界对象的 Step 方法中即可，同时我们需要遍历世界中的一切对象，并对每个对象的坐标和角度进行更新。</p> <h2 id="b2world"><a href="#b2world" aria-hidden="true" class="header-anchor">#</a> b2World</h2> <h3 id="介绍"><a href="#介绍" aria-hidden="true" class="header-anchor">#</a> 介绍</h3> <p>b2World 类包含着物体和关节。它管理着模拟的方方面面，并允许异步查询(就像 AABB 查询)。你与 Box2D 的大部分交互都将通过 b2World 对象来完成。</p> <p>创建一个世界十分的简单。你只需提供一个包围盒和一个重力向量。</p> <p>轴对齐包围盒(AABB)应该包围着世界。稍微比世界大一些的包围盒可以提升性能，比方 2 倍大小才安全。如果你的许多物体都掉进了深渊，你应该侦测并移除它们。这能提升性能并预防浮点溢出。</p> <h3 id="创建一个世界"><a href="#创建一个世界" aria-hidden="true" class="header-anchor">#</a> 创建一个世界</h3> <p>创建一个世界，并设置其有效区域的大小，重力大小及方向，以及是否允许休眠等。</p> <ol><li>定义有效区域大小：创建一个<code>b2AABB</code>类，然后设定其左上角及右下角坐标。有效区域即 box2d 可以发挥作用，反映各种物理规律的区域。</li></ol> <div class="language-javascript extra-class"><pre class="language-javascript"><code>worldAABB <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2AABB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
worldAABB<span class="token punctuation">.</span>minVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1000</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
worldAABB<span class="token punctuation">.</span>maxVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token number">1200</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>注意 ： worldAABB 应该永远比物体所在的区域要大，让 worldAABB 更大总比太小要好。如果一个物体到达了 worldAABB 的边界，它就会被冻结并停止模拟。</p> <ol start="2"><li>设置重力大小及方向：通过定义一个二维矢量来实现，创建一个<code>b2Vec2</code>类，并在括号中给出其 x，y 方向的大小。</li></ol> <div class="language-javascript extra-class"><pre class="language-javascript"><code>gravity <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>备注：gravity 是重力加速度。</p> <ol start="3"><li>设置世界是否会休眠：true 即允许休眠，false 不允许休眠。当世界被设置为允许休眠时，物体静止一段时间后会被判定为休眠，直到有碰撞发生或者人为激活。一个休眠中的物体不需要任何模拟。</li></ol> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> doSleep <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
</code></pre></div><ol start="4"><li>创建世界：利用<code>b2World</code>创建拥有上述性质的世界。</li></ol> <div class="language-javascript extra-class"><pre class="language-javascript"><code>world <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2World</span><span class="token punctuation">(</span>worldAABB<span class="token punctuation">,</span> gravity<span class="token punctuation">,</span> doSleep<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="创建一个世界的完整代码"><a href="#创建一个世界的完整代码" aria-hidden="true" class="header-anchor">#</a> 创建一个世界的完整代码</h3> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> worldAABB <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2AABB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
worldAABB<span class="token punctuation">.</span>minVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1000</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
worldAABB<span class="token punctuation">.</span>maxVertex<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> gravity <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2Vec2</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> doSleep <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> world <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">b2World</span><span class="token punctuation">(</span>worldAABB<span class="token punctuation">,</span> gravity<span class="token punctuation">,</span> doSleep<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></div> <!----> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/godbasin/front-end-playground/edit/sourcecode/docs/front-end-others/box2d-tutorial/2-0-world.md" target="_blank" rel="noopener noreferrer">帮阿猪改善此页面！</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <!----> <blockquote>部分文章中使用了一些网站的截图，如果涉及侵权，请告诉我删一下谢谢~</blockquote> <div style="margin-top:30px;"><div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-sm-0 el-col-md-2 el-col-lg-4" style="padding-left:10px;padding-right:10px;display:block;"><div style="width:1px;height:1px;"></div></div> <div class="el-col el-col-24 el-col-sm-24 el-col-md-18 el-col-lg-16" style="padding-left:10px;padding-right:10px;"><div class="el-card box-card is-always-shadow"><div class="el-card__header"><div class="clearfix"><span>温馨提示喵</span></div></div><div class="el-card__body"> <div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="el-image"><div class="image-slot"><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/loading.gif" style="width:100%;"></div><!----></div></div> <div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="copyright-text"><div>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</div> <div>出处：被删的前端游乐场</div> <div>作者：<a href="https://github.com/godbasin" target="_blank">被删</a></div></div></div></div></div></div></div></div></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/front-end-playground/front-end-others/box2d-tutorial/1-0-basic-catalog.html" class="prev">
          1. 基本概念
        </a></span> <span class="next"><a href="/front-end-playground/front-end-others/box2d-tutorial/3-0-shape-and-body.html">
          3. 形状 shape - 刚体 body
        </a>
        →
      </span></p></div>  <div class="gitalk-container theme-default-content"><div id="gitalk-container" class="content"></div></div></main> <div id="kitty-container"><span><div role="tooltip" id="el-popover-9393" aria-hidden="true" class="el-popover el-popper" style="width:undefinedpx;display:none;"><!----><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" class="image"> <div class="text">牡羊猪的猫粮罐</div> </div><div id="kitty" style="background:url(https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/kitty0.svg);"></div></span> <div class="el-dialog__wrapper" style="display:none;"><div role="dialog" aria-modal="true" aria-label="牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）" class="el-dialog" style="margin-top:15vh;"><div class="el-dialog__header"><span class="el-dialog__title">牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）</span><button type="button" aria-label="Close" class="el-dialog__headerbtn"><i class="el-dialog__close el-icon el-icon-close"></i></button></div><!----><!----></div></div></div></div><div class="global-ui"></div></div>
    <script src="/front-end-playground/assets/js/app.e5cf4092.js" defer></script><script src="/front-end-playground/assets/js/3.c853767c.js" defer></script><script src="/front-end-playground/assets/js/2.eed49fdd.js" defer></script><script src="/front-end-playground/assets/js/63.d0b42c5b.js" defer></script>
  </body>
</html>
